package at.jku.analyzer.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import at.jku.analyzer.interfaces.AnalyzerModule;
import at.jku.analyzer.modules.JavaAnalyzer;

public class AnalyzerCore {

	/** K: extension, V: classname */
	private static ArrayList<String> modules;

	public static List<String> getModules() {
		return modules;
	}

	static {
		modules = new ArrayList<>();
		modules.add(".java");
	}


	public static List<ResultsObj> execute(String absolutepath, OptionsObj obj)
			throws IOException {
		ArrayList<ResultsObj> results = new ArrayList<>();

		File location = new File(absolutepath);
		if (!location.exists()) {
		}
		if (location.isFile()) {
			results.add(executeAnalyze(location, obj));

		} else if (location.isDirectory()) {
			executeRec(location, obj, results);
		}

		return results;
	}

	private static void executeRec(File f, OptionsObj obj, List<ResultsObj> list)
			throws IOException {
		File[] dir = f.listFiles();
		for (File loc : dir) {
			if (loc.isFile()) {
				list.add(executeAnalyze(loc, obj));
			} else {
				executeRec(loc, obj, list);
			}
		}
	}

	private static ResultsObj executeAnalyze(File file, OptionsObj obj)
			throws IOException {

		StringBuilder str = new StringBuilder(file.getAbsolutePath());
		str.delete(0, str.lastIndexOf("."));

		String extension = str.toString();

		AnalyzerModule am = null;
		switch (extension) {
		case ".java":
			am = new JavaAnalyzer(file);
			break;
		case ".cpp":
			break;
		case ".html":
			break;

		}

		int linesOfComments = 0, loc = 0;
		String absolutPath = file.getAbsolutePath();
		Map<String, Integer> numOfKeyword = null;
		Map<String, Integer> numOfWords = null;

		if (am != null) {

			if (obj.isLinesofCommentsSelected()) {
				linesOfComments = am.getLinesOfComments();
			}
			if (obj.isLocSelected()) {
				loc = am.getLoC();
			}
			if (obj.isNumOfKeywordsSelected()) {
				numOfKeyword = am.getNumOfKeywords();
			}
			if (obj.isNumofWordsSelected()) {
				numOfWords = am.getNumOfWords();
			}
		}
		return new ResultsObj(linesOfComments, loc, absolutPath, numOfKeyword,
				numOfWords);

	}

}
